package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.ThresholdDao;
import cn.web.tunnelrobot.dao.UserDao;
import cn.web.tunnelrobot.dao.UserRecordDao;
import cn.web.tunnelrobot.dao.WarningRecordDao;
import cn.web.tunnelrobot.dto.EarlyWarning;
import cn.web.tunnelrobot.dto.WarningStatistics;
import cn.web.tunnelrobot.pojo.Threshold;
import cn.web.tunnelrobot.pojo.WarningRecord;
import cn.web.tunnelrobot.service.ThresholdService;
import cn.web.tunnelrobot.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ThresholdServiceImpl implements ThresholdService {
    @Autowired
    private ThresholdDao thresholdDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRecordDao userRecordDao;
    @Autowired
    private WarningRecordDao warningRecordDao;

    @Override
    public HashMap<String, Object> updateBatchThreshold(List<Threshold> thresholdList) {
        HashMap<String,Object> map = new HashMap<String,Object>();
//        if(Constants.is_intranet == 1){
//            //内网不提供编辑权限
//            map.put("code",-1);
//            map.put("msg","内网没有权限");
//            return map;
//        }
        int result = thresholdDao.updateBatchThreshold(thresholdList);
        if (result > 0){
            Constants.list = thresholdDao.getThresholdList(null);
            Constants.temperature.clear();
            Constants.humidity.clear();
            Constants.oxygenConcentrations.clear();
            Constants.dustConcentration.clear();
            Constants.gasDensity.clear();
            Constants.batteryWarning.clear();
            for (Threshold threshold : Constants.list) {
                if (threshold.getThWarningSign() == 1) {
                    Constants.temperature.add(threshold);
                } else if (threshold.getThWarningSign() == 2) {
                    Constants.humidity.add(threshold);
                } else if (threshold.getThWarningSign() == 3) {
                    Constants.oxygenConcentrations.add(threshold);
                }  else if (threshold.getThWarningSign() == 4) {
                    Constants.dustConcentration.add(threshold);
                } else if (threshold.getThWarningSign() == 5) {
                    Constants.gasDensity.add(threshold);
                } else if (threshold.getThWarningSign() == 6) {
                    Constants.batteryWarning.add(threshold);
                }else if(threshold.getThWarningSign() == 7){
                    Constants.settle = threshold;
                } else if(threshold.getThWarningSign() == 8){
                    Constants.convergence = threshold;
                }else if(threshold.getThWarningSign() == 9){
                    Constants.cloudTurnAngle= threshold;
                }
            }
            map.put("code",0);
            map.put("msg","设置成功");
           /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user2.getUsId());
            userRecord.setUrOperatContent("修改阈值设置信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
        }else {
            map.put("code",-1);
            map.put("msg","设置失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> getThresholdList(Integer thWarningSign) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<Threshold> list = thresholdDao.getThresholdList(thWarningSign);
        map.put("code",0);
        map.put("data",list);
       /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user2 = (User) request.getSession().getAttribute(Constants.USER_SESSION);
        UserRecord userRecord = new UserRecord();
        userRecord.setUsId(user2.getUsId());
        userRecord.setUrOperatContent("查询阈值设置信息");
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*/
        return map;
    }

    @Override
    public HashMap<String, Object> getWarningStatistics() {
        HashMap<String,Object> map = new HashMap<String,Object>();
        //一级警告
        Integer count11 = thresholdDao.getThresholdListByLevelHandled("Ⅰ",1);
        Integer count12 = thresholdDao.getThresholdListByLevelHandled("Ⅰ",0);
        //二级警告
        Integer count21 = thresholdDao.getThresholdListByLevelHandled("Ⅱ", 1);
        Integer count22 = thresholdDao.getThresholdListByLevelHandled("Ⅱ", 0);
        //三级警告
        Integer count31 = thresholdDao.getThresholdListByLevelHandled("Ⅲ", 1);
        Integer count32 = thresholdDao.getThresholdListByLevelHandled("Ⅲ", 0);
        //四级警告
        Integer count41 = thresholdDao.getThresholdListByLevelHandled("Ⅳ", 1);
        Integer count42 = thresholdDao.getThresholdListByLevelHandled("Ⅳ", 0);
        //五级警告
        Integer count51 = thresholdDao.getThresholdListByLevelHandled("Ⅴ", 1);
        Integer count52 = thresholdDao.getThresholdListByLevelHandled("Ⅴ", 0);

        map.put("one",count11);
        map.put("noOne",count12);
        map.put("two",count21);
        map.put("noTwo",count22);
        map.put("three",count31);
        map.put("noThree",count32);
        map.put("four",count41);
        map.put("noFour",count42);
        map.put("five",count51);
        map.put("noFive",count52);
        map.put("code",0);
        return map;
    }
    @Override
    public HashMap<String, Object> getEarlyWarning() {
        HashMap<String,Object> map = new HashMap<String,Object>();
        List<EarlyWarning> list = warningRecordDao.getEarlyWarning();
        if(list.size() > 0){
            int[] number= new int[7];
            String[] time= new String[7];
            for (int i=0; i < list.size(); i++) {
                number[i] = list.get(i).getNumber();
                time[i] = list.get(i).getTime().substring(5);
            }
            map.put("code",0);
            map.put("number",number);
            map.put("time",time);
        }else {
            map.put("code",-1);
            map.put("msg","暂无信息");
        }
        return map;
    }
}
